package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.OrderGoods;
import com.fbee.smartcommunities.service.OrderGoodsService;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.vo.OrderGoodsVO;
import com.fbee.smartcommunities.vo.OrderVO;
import com.fbee.smartcommunities.vo.ResultVO;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 订单商品关系表 前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-27
 */
@RestController
@RequestMapping("/orderGoods")
public class OrderGoodsController {

    @Resource
    private OrderGoodsService orderGoodsService;

    @GetMapping("/getOrders")
    ResultVO<List<OrderVO>> getOrders(Long openId){
        return ResultVO.success(orderGoodsService.getOrders(openId));
    }

    @PostMapping("/trade")
    ResultVO<Boolean> trade(Long openId, Long to, Double count, Long id, Integer sum, Long goodsId, HttpServletRequest request){
        return ResultVO.success(orderGoodsService.trade(openId,to,count,id,sum,goodsId,request),"付款成功");
    }

    @PostMapping("/delete")
    ResultVO<Boolean> delete(Long id){
        return ResultVO.success(orderGoodsService.delete(id),"订单移除成功");
    }

    @GetMapping("/getOrdersByCid")
    ResultVO<IPage<OrderGoodsVO>> getOrdersByCid(Long openId, String key, Integer pageNo, Integer pageSize){
        return ResultVO.success(orderGoodsService.getOrdersByCid(openId,key,pageNo,pageSize));
    }

    @PostMapping("/changeState")
    ResultVO<Boolean> changeState(Long id,Integer state){
        return ResultVO.success(orderGoodsService.update(new UpdateWrapper<OrderGoods>().eq("id",id).set("state",state)));
    }
}
